# 分析
# 发现数据在html 页面中已经渲染  可以通过获取标签  提取数据
# 发现在 script 标签中  有全部的 json 格式的数据  可以直接去获取
# 使用正则  re.findall 提取数据

import json
import re
import requests


class Kr36:
    def __init__(self):
        # 1准备起始url 和请求头信息
        self.url = "https://36kr.com/"
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36',
        }

    def get_data(self):
        # 发送请求获取响应
        response = requests.get(self.url, headers=self.headers)
        return response.content.decode('utf-8')

    def parse_data(self, data):
        # 正则表达式提取数据
        # 贪婪
        result = re.findall('<script>window.initialState=(.*?)</script>', data)[0]
        # print(result)
        # 把数据转为字典
        data_dict = json.loads(result)
        # print(data_dict)
        data_list = data_dict['homeData']['data']['homeFlow']['data']['itemList']
        print(data_list)
        new_data_list = []
        for item in data_list:
            # itemType 不同 新闻的类型不同  数据10 是最新的新闻
            if item['itemType'] == 10:
                item = item['templateMaterial']
                temp = {}
                temp['title'] = item.get('widgetTitle')
                temp['time'] = item.get('publishTime')
                temp['authorName'] = item.get('authorName')
                temp['image'] = item.get('widgetImage')
                temp['summary'] = item.get('summary')
                new_data_list.append(temp)

        return new_data_list

    def save_data(self, data_list):
        for data in data_list:
            print(data)

    def start(self):
        data = self.get_data()
        data_list = self.parse_data(data)
        self.save_data(data_list)


if __name__ == '__main__':
    d = Kr36()
    d.start()
